//
//  LikeContainer2.swift
//  LikeContainer2
//
//  Created by 劢克科技 on 2019/5/29.
//  Copyright © 2019 meckey. All rights reserved.
//

import UIKit

class LikeContainer2: NSObject {
    //MARK: - Getter
    
    lazy var likeContainerView: UIView = {
        let view = UIView()
        let images: [UIImage] = [#imageLiteral(resourceName: "blue_like"), #imageLiteral(resourceName: "red_heart"), #imageLiteral(resourceName: "cry_laugh"), #imageLiteral(resourceName: "surprised"), #imageLiteral(resourceName: "cry"), #imageLiteral(resourceName: "angry")]
        let margin: CGFloat = 5
        let itemSize: CGFloat = 36
        
        let arrangedSubs = images.map({ (image) -> UIImageView in
            let imageView = UIImageView()
            imageView.image = image
            imageView.isUserInteractionEnabled = true
            return imageView
        })
        
        let stackView = UIStackView(arrangedSubviews: arrangedSubs)
        stackView.distribution = .fillEqually
        stackView.spacing = margin
        stackView.layoutMargins = UIEdgeInsets(top: margin, left: margin, bottom: margin, right: margin)
        stackView.isLayoutMarginsRelativeArrangement = true
        
        view.backgroundColor = UIColor.white
        view.layer.cornerRadius = (itemSize + margin * 2) / 2
        view.layer.shadowRadius = 6
        view.layer.shadowOffset = CGSize(width: 0, height: 4)
        view.layer.shadowOpacity = 0.4
        
        view.frame = CGRect(x: 0, y: 0, width: (CGFloat(arrangedSubs.count) * itemSize) + CGFloat(arrangedSubs.count + 1) * margin, height: itemSize + 2 * margin)
        stackView.frame = view.bounds
        view.addSubview(stackView)
        return view
    }()
    
    //MARK: - Setter
    
    fileprivate var view: UIView!
    
    //MARK: - Events
    
    // 注册View
    func register(view: UIView) {
        let gesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(gesture:)))
        view.addGestureRecognizer(gesture)
        self.view = view
    }
    
    @objc fileprivate func handleLongPress(gesture: UILongPressGestureRecognizer) {
        
        if gesture.state == .began {
            pressBegan(gesture: gesture)
        } else if gesture.state == .ended {
            pressEnd(gesture: gesture)
        } else if gesture.state == .changed {
            pressChange(gesture: gesture)
        }
    }
    
    // 执行长按结束动画
    fileprivate func pressEnd(gesture: UILongPressGestureRecognizer) {
        
        UIView.animate(withDuration: 0.25, animations: {
            let stack = self.likeContainerView.subviews.first
            stack?.subviews.forEach({ (imageView) in
                imageView.transform = .identity
            })
            
            self.likeContainerView.transform = self.likeContainerView.transform.translatedBy(x: 0, y: 50)
            self.likeContainerView.alpha = 0
        }) { (_) in
            self.likeContainerView.removeFromSuperview()
        }
    }
    
    // 执行长按开始动画
    fileprivate func pressBegan(gesture: UILongPressGestureRecognizer) {
        let location = gesture.location(in: UIApplication.shared.keyWindow!)
        let x = (UIScreen.main.bounds.width - likeContainerView.frame.width) / 2
        let y = location.y - likeContainerView.frame.height / 2
        let finalY = location.y - likeContainerView.frame.height - 30
        UIApplication.shared.keyWindow!.addSubview(likeContainerView)
        likeContainerView.alpha = 0
        likeContainerView.transform = CGAffineTransform(translationX: x, y: y)
        
        UIView.animate(withDuration: 0.25) {
            self.likeContainerView.alpha = 1
            self.likeContainerView.transform = CGAffineTransform(translationX: x, y: finalY)
        }
    }
    
    // 执行长按移动动画
    fileprivate func pressChange(gesture: UILongPressGestureRecognizer) {
        let location = gesture.location(in: likeContainerView)
        
        let fixYLocation = CGPoint(x: location.x, y: likeContainerView.frame.height / 2)
        let hitTestView = likeContainerView.hitTest(fixYLocation, with: nil)
        
        if hitTestView is UIImageView {
            UIView.animate(withDuration: 0.25) {
                let stackView = self.likeContainerView.subviews.first
                stackView?.subviews.forEach({ (image) in
                    image.transform = .identity
                })
                hitTestView?.transform = CGAffineTransform(translationX: 0, y: -50)
            }
        }
    }
}
